package admin.forms;

import helpers.StringHelper;
import helpers.UrlHelper;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import admin.models.ApplicationMappingModel;

import entities.Application;
import entities.ApplicationMapping;

public class ApplicationMappingForm {
	public static TreeMap checkValid(HttpServletRequest request, ApplicationMapping appMapping) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String domain = (String) request.getParameter("domain");
		String applicationId = (String) request.getParameter("applicationId");
		
		ApplicationMappingModel appMappingModel = ApplicationMappingModel.getInstance();
		
		if (domain == null || domain.isEmpty()) {
			check.put("domain", "Please enter domain value.");
			appMapping.setDomain("");
		} else if (!UrlHelper.checkDomain(domain)) {
			check.put("domain", "Please enter domain valid.");
			appMapping.setDomain(domain);
		} else if (appMappingModel.checkExists(domain)) {
			check.put("domain", "Domain has been exists.");
			appMapping.setDomain(domain);
		} else {
			appMapping.setDomain(domain);
		}
		
		if (applicationId != null && applicationId.isEmpty()) {
			appMapping.setApplicationId(applicationId);
		}
		
		
		form.put("checkValid", check);
		form.put("appMapping", appMapping);
		
		return form;
	}
}
